home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 15 / macformat_15.iso / Shareware Internet / Desarrolladores / gray image 2.1 / vmorph_filter.cc < prev    next >
C/C++ Source or Header  |  1995-05-31  |  6KB  |  199 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *           Morphological filtration of the image
  8.  *    
  9.  *                Verification
  10.  *
  11.  * $Id$
  12.  *
  13.  ************************************************************************
  14.  */
  15.  
  16. #include "morph_filter.h"
  17.  
  18. #include <iostream.h>
  19.  
  20.  
  21.                 // Verify the morphological filtration
  22.                 // for the pattern of size 1
  23. static void test_pattern_size1(void)
  24. {
  25.   IMAGE im(7,7,8);
  26.   IMAGE imt(im);
  27.   BinaryPattern pattern(Square,1);
  28.  
  29.   cout << "\n\nMake sure that each morphological filtration operation"
  30.           "\nwith the square pattern of size 1 yields the identical image\n";
  31.  
  32.   cout << "\nTest image is " << im.q_nrows() << "x" << im.q_ncols() <<
  33.           "x" << im.q_depth();
  34.  
  35.   im.clear();
  36.   im.square_of(2,rowcol(2,2)) = 2;
  37.   imt = im;
  38.   assert( (erosion(imt,pattern), imt == im) );
  39.   assert( (dilation(imt,pattern), imt == im) );
  40.   assert( (opening(imt,pattern), imt == im) );
  41.   assert( (closing(imt,pattern), imt == im) );
  42.  
  43.   im.clear();
  44.   im.square_of(3,rowcol(2,2)) = 2;
  45.   imt = im;
  46.   assert( (erosion(imt,pattern), imt == im) );
  47.   assert( (dilation(imt,pattern), imt == im) );
  48.   assert( (opening(imt,pattern), imt == im) );
  49.   assert( (closing(imt,pattern), imt == im) );
  50.  
  51.  
  52.   im.clear();
  53.   im.square_of(4,rowcol(2,2)) = 2;
  54.   imt = im;
  55.   assert( (erosion(imt,pattern), imt == im) );
  56.   assert( (dilation(imt,pattern), imt == im) );
  57.   assert( (opening(imt,pattern), imt == im) );
  58.   assert( (closing(imt,pattern), imt == im) );
  59.  
  60.  
  61.   im(3,3) = 1; im(3,4) = 0;
  62.   imt = im;
  63.   assert( (erosion(imt,pattern), imt == im) );
  64.   assert( (dilation(imt,pattern), imt == im) );
  65.   assert( (opening(imt,pattern), imt == im) );
  66.   assert( (closing(imt,pattern), imt == im) );
  67.  
  68.   cout << "\nDone\n";
  69. }
  70.  
  71.  
  72.                 // Check out to see whether opening and
  73.                 // closing of the image containing boxes
  74.                 // with the box of the same size change
  75.                 // nothing
  76.  
  77. static void test_equal_by_equal_1(const int size)
  78. {
  79.   cout << "\nMake sure that opening and closing a box with the box of the same"
  80.           "\nsize changes nothing" << endl;
  81.   
  82.   cout << "\nBox of the size " << size << " at the upper left corner"
  83.           "\nof an image " << 2*size+1 << 'x' << 2*size+2 << endl;
  84.  
  85.   IMAGE im(2*size+1,2*size+2,8);
  86.   BinaryPattern pattern(Square,size);
  87.   im.clear();
  88.   im.square_of(size,rowcol(0,0)) = 2;
  89.   IMAGE imt(im), im_dil(im), im_er(im);
  90.  
  91.   im_dil.square_of(2*size-1,rowcol(0,0)) = 2;
  92.   cout << "\nDilated box should have size " << 2*size+1;
  93.   assert( (imt=im, dilation(imt,pattern), imt == im_dil) );
  94.  
  95.   im_er.square_of(1,rowcol(0,0)) = 2;
  96.   cout << "\nErosed box should be reduced to a one point";
  97.   assert( (imt=im, erosion(imt,pattern), imt == im_er) );
  98.  
  99.   cout << "\nClosing and opening should keep the box the same";
  100.   assert( (imt=im, opening(imt,pattern), imt == im) );
  101.   assert( (imt=im, closing(imt,pattern), imt == im) );
  102.  
  103.   cout << "\nDone\n";
  104. }
  105.  
  106. static void test_equal_by_equal_2(const int size)
  107. {
  108.   cout << "\nMake sure that opening and closing a box with the box of the same"
  109.           "\nsize changes nothing. But the image border has to be "
  110.       "kept in mind" << endl;
  111.   
  112.   cout << "\nBox of the size " << size << " at the lower right corner"
  113.           "\nof an image " << 2*size+1 << 'x' << 2*size+2 << endl;
  114.  
  115.   IMAGE im(2*size+1,2*size+2,8);
  116.   BinaryPattern pattern(Square,size);
  117.  
  118.   im.clear();
  119.   im.square_of(size,rowcol(size+1,size+2)) = 3;
  120.   IMAGE imt(im), im_dil(im), im_er(im);
  121.  
  122.   im_dil = im;
  123.   cout << "\nDilated box is the same as the original one due to the clipping";
  124.   assert( (imt=im, dilation(imt,pattern), imt == im_dil) );
  125.  
  126.   im_er.square_of(1,rowcol(size+1,size+2)) = 3;
  127.   cout << "\nErosed box should be reduced to a one point";
  128.   assert( (imt=im, erosion(imt,pattern), imt == im_er) );
  129.  
  130.   cout << "\nOpening is the same as erasing now";
  131.   assert( (imt=im, opening(imt,pattern), imt == im_er) );
  132.  
  133.   cout << "\nClosing should keep the box the same";
  134.   assert( (imt=im, closing(imt,pattern), imt == im) );
  135.  
  136.   cout << "\nDone\n";
  137. }
  138.  
  139.                 // Check to see that in case of the filter with
  140.                 // the size larger than the image box,
  141.                 // erosing eroses the box altogether,
  142.                 // and so does closing.
  143.                 // On the opposite, dilation expands
  144.                 // the box to the size n1+n2-1, and
  145.                 // opening returns the box to the original
  146.                 // size
  147.                 // Opening and closing boxes larger than the
  148.                 // filter size should keep them intact.
  149. static void test_nonequal(const int size)
  150. {
  151.   cout << "\nVerifying filtration of boxes of different size" << endl;
  152.   
  153.   cout << "\nTwo boxes of the size " << size-1 << " and " << size+1 <<
  154.           "\nin an image " << 4*size+3 << 'x' << 4*size+5 << endl;
  155.  
  156.   IMAGE im(5*size+3,5*size+5,8);
  157.   BinaryPattern pattern(Square,size);
  158.  
  159.   im.clear();
  160.   im.square_of(size-1,rowcol(1,1)) = 4;
  161.   im.square_of(size+1,rowcol(3*size+1,2*size+3)) = 1;
  162.   IMAGE imt(im), im_er(im), im_dil(im), im_cl(im);
  163.  
  164.                 // Theoretical dilated image
  165.   im_dil.square_of(2*size-2,rowcol(1,1)) = 4;
  166.   im_dil.square_of(2*size,rowcol(3*size+1,2*size+3)) = 1;
  167.   cout << "\nDilated boxes should be expanded ";
  168.   assert( (imt=im, dilation(imt,pattern), imt == im_dil) );
  169.  
  170.                 // Theoretical erosed image
  171.   im_er.square_of(2,rowcol(3*size+1,2*size+3)) = 1;
  172.   cout << "\nSmall box should disappear on erosion";
  173.   assert( (imt=im, erosion(imt,pattern), imt == im_er) );
  174.  
  175.                 // Theoretical opened image
  176.   cout << "\nOpening should keep the boxes intact";
  177.   assert( (imt=im, opening(imt,pattern), imt == im) );
  178.  
  179.   cout << "\nClosing erases the smaller box";
  180.   im_cl.square_of(size+1,rowcol(3*size+1,2*size+3)) = 1;
  181.   assert( (imt=im, closing(imt,pattern), imt == im_cl) );
  182.  
  183.   cout << "\nDone\n";
  184. }
  185.  
  186.                 // Root module
  187. main()
  188. {
  189.   test_pattern_size1();
  190.  
  191.   test_equal_by_equal_1(2);
  192.   test_equal_by_equal_1(5);
  193.   test_equal_by_equal_2(5);
  194.   test_equal_by_equal_2(15);
  195.  
  196.   test_nonequal(2);
  197.   test_nonequal(5);
  198. }
  199.